﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BiliBiliCSharpStudy.old {
    class CheckDemo {
        public static void demo() {
            //test1();
            test2();
        }

        public static void test2() {
            uint x = uint.MaxValue;
            checked {
                try {
                    uint y1 = checked(x + 1);
                    Console.WriteLine(y1);
                } catch (OverflowException ex) {
                    Console.WriteLine("There is overflow!");
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public static void test1() {
            uint x = uint.MaxValue;
            Console.WriteLine(x);
            string binStr = Convert.ToString(x, 2);
            Console.WriteLine(binStr);

            // 溢出
            uint y = x + 1;
            Console.WriteLine(y);

            // 检测
            try {
                // checked , unchecked
                uint y1 = checked(x + 1);
                Console.WriteLine(y1);
            } catch (OverflowException ex) {
                Console.WriteLine("There is overflow!");
            }
        }
    }
}
